import { platform } from "@/util/platform";

const updateManager = uni?.getUpdateManager && uni.getUpdateManager();
function updateMiniProgram() {
  updateManager.onCheckForUpdate((res) => {
    if (res.hasUpdate) {
      updateManager.onUpdateReady(() => {
        uni.showModal({
          title: "更新提示",
          showCancel: true,
          cancelText: "取消",
          confirmText: "确定",
          content: "新版本已经准备好，是否重启应用?",
          success: (res) => {
            res.confirm && updateManager.applyUpdate();
          },
        });
      });
      updateManager.onUpdateFailed(() => {
        uni.showModal({
          title: "温馨提示",
          showCancel: true,
          cancelText: "取消",
          confirmText: "确定",
          content: "新版本已经上线，请您删除当前小程序，重新搜索打开",
        });
      });
    }
  });
}

function updateApp() {
  switch (platform) {
    case "H5": {
      break;
    }
    case "APP-PLUS":
    case "APP-NVUE": {
      break;
    }
    default: {
      updateMiniProgram();
      break;
    }
  }
}

export { updateApp };
